<?php
/*
Example Uses:

Grab all fields from a table
$mnu = new menu(STATES_TABLE);
$menu = $mnu->menu_from_db('evt_state','st_abbrev','st_abbrev',$evt->values['evt_state'],'-State-');

Grab specific field(s) fields from a table with WHERE condition
$mnu = new menu(HOUSE_VOLUNTEER_POSITIONS,'vol_id,vol_position','vol_active=1');
$menu = $mnu->menu_mult_from_db('hous_volunteer','dummy_id',$volunteer_positions,unserialize($hous->values['hous_volunteer']));

Make wild and crazy query
$mnu = new menu();
$mnu->query = "some impossibly complicated query";
$menu = $mnu->menu_mult_from_db('hous_volunteer','dummy_id',$volunteer_positions,unserialize($hous->values['hous_volunteer']));

*/

class menu {

// constructor
//////////////////////////////////////////////////////////////////////////////////////
function menu($table='',$fields='',$where='',$order_by='') {
     if( !$fields) {
        // get em all
        $fields = '*';
     }
     if( !$where ) {
        $where = '1';
     }
    
     $this->query = "SELECT $fields FROM $table WHERE $where";
}



//////////////////////////////////////////////////////////////////////////////////////
function menu_from_db($name,$value_field,$text_field,$default="",$dummy_option="",$onclick='') {
	  
	   $menu = "<select name=\"$name\" onclick=\"$onclick\" >\n";
	   
	   if ($dummy_option) {
	        //  to at least make them choose from the menu
	        // so they dont just leave the first one selected
	        $menu .= "<option value=\"\" ";
	        if ($default == '' ) {
                $menu .= "selected='yes'";
            }
            $menu .= ">$dummy_option</option>\n";
	    }
        
        $result = app_data::mysql_query($this->query);
        while ($row = mysql_fetch_array($result)) {
            $menu .= "<option value=\"$row[$value_field]\" ";
            if ($default == $row[$value_field] ) {
                $menu .= "selected='yes'";
            }
            $menu .= ">$row[$text_field]</option>\n";
       }
       $menu .= "</select>\n";
       return $menu;
} 

//////////////////////////////////////////////////////////////////////////////////////
function menu_mult_from_db($name,$id,$value_field,$text_field,$default='',$size='5') {
    // the id is needed for javascript validation since php needs the name to 
    // have [], and that messes up javascript object references (unless you are clever!)
    
    if ($default == '') {
        $default = serialize(array());
    }
    $default_values = unserialize(stripslashes($default));

    $menu = "<select name=\"$name\" id=\"$id\" multiple=\"yes\" size=\"$size\">\n\n";
    
    $result = app_data::mysql_query($this->query);
    while ($row = mysql_fetch_array($result)) {
        $menu .= "<option value=\"$row[$value_field]\" ";
        if (in_array($row[$value_field],$default_values) ) {
            $menu .= "selected='yes'";
        }

        $menu .= ">$row[$text_field]</option>\n";
     }
    $menu .= "</select>\n";
    return $menu;
} 

//////////////////////////////////////////////////////////////////////////////////////
function menu_from_array($name,&$array,$default="",$dummy_option="") {
	  
	   $menu = "<select name=\"$name\" >\n";
	   
	   if ($dummy_option) {
	        //  to at least make them choose from the menu
	        // so they dont just leave the first one selected
	        $menu .= "<option value=\"\" ";
	        if ($default == '' ) {
                $menu .= "selected='yes'";
            }
            $menu .= ">$dummy_option</option>\n";
	    }
	  
       foreach ($array as $a) {
            $menu .= "<option value=\"$a\" ";
            if ($default == $a ) {
                $menu .= "selected='yes'";
            }
            $menu .= ">$a</option>\n";
       }
       $menu .= "</select>\n";
       return $menu;
} 

//////////////////////////////////////////////////////////////////////////////////////
function menu_from_assoc_array($name,&$array,$default="",$dummy_option="",$other='') {
      // the keys become the menu values the values become the menu text
	  
	   $menu = "<select name=\"$name\" $other>\n";
	   
	   if ($dummy_option) {
	        //  to at least make them choose from the menu
	        // so they dont just leave the first one checked
	        $menu .= "<option value=\"\" ";
	        if ($default == '' ) {
                $menu .= "selected='yes'";
            }
            $menu .= ">$dummy_option</option>\n";
	    }
	  
       foreach ($array as $key=>$value) {
            $menu .= "<option value=\"$key\" ";
            if ($default == $key ) {
                $menu .= "selected='yes'";
            }
            $menu .= ">$value</option>\n";
       }
       $menu .= "</select>\n";
       return $menu;
} 

    
//////////////////////////////////////////////////////////////////////////////////////
function menu_mult_from_array($name,$id,&$array,$default='') {
    if ($default == '') {
        $default = serialize(array());
    }
    $default_values = unserialize(stripslashes($default));

    // the id is useful for javascript validation since php needs the name to 
    // have [], and that messes up javascript object references
    $menu = "<select name=\"$name\" id=\"$id\" multiple=\"yes\" size=\"".count($array)."\">\n\n";
    foreach ($array as $a) {
        $menu .= "<option value=\"$a\" ";
        if (in_array($a,$default_values) ) {
            $menu .= "selected='yes'";
        }

        $menu .= ">$a</option>\n";
     }
    $menu .= "</select>\n";
    return $menu;
} 

//////////////////////////////////////////////////////////////////////////////////////
function menu_mult_from_assoc_array($name,$id,&$array,$default='') {
    if ($default == '') {
        $default = array();
    }
    $default_values = $default;
    // the id is useful for javascript validation since php needs the name to 
    // have [], and that messes up javascript object references
    $menu = "<select name=\"$name\" id=\"$id\" multiple=\"yes\" size=\"".count($array)."\">\n\n";
    foreach ($array as $key=>$value) {
        $menu .= "<option value=\"$key\" ";
        if (in_array($key,$default_values) ) {
            $menu .= "selected='yes'";
        }

        $menu .= ">$value</option>\n";
     }
    $menu .= "</select>\n";
    return $menu;
} 

// static method
//////////////////////////////////////////////////////////////////////////////////////
function process_multiple_select(&$object,$field_name) {
    // takes the submitted array and serializes it into a string before assigning it to the object field
         if (isset($_REQUEST[$field_name])) {
            $array_as_text = addslashes(serialize($_REQUEST[$field_name]));
        }
        else {
            $array_as_text = array();
            $array_as_text = addslashes(serialize($array_as_text));
        }
        $object->values[$field_name] = $array_as_text;
} // end process_multiple_select function

 

// static method	
//////////////////////////////////////////////////////////////////////////////////////
function integer_menu($name,$start,$stop,$default="",$dbl_digits="",$dummy_option='') {
    $menu = "<select name=\"$name\" >\n";
    
    if ($dummy_option) {
        //  to at least make them choose from the menu
        // so they dont just leave the first one checked
        $menu .= "<option value=\"\" ";
        if ($default == '' ) {
            $menu .= "selected='yes'";
        }
        $menu .= ">$dummy_option</option>\n";
    }
    
    for ($i=$start ; $i<= $stop ; $i++ ) {
        if ( $i<10 && $dbl_digits ) {
            $i = '0'.$i;
        }
        $menu .= "<option value=\"$i\" ";
        if ($default == $i ) {
            $menu .= "selected='yes'";
        }
        $menu .= ">$i</option>\n";
     }
    $menu .= "</select>\n";
    return $menu;
} 

	
	
}//end class menu

?>
